package com.zz.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@RestController
public class FileController {
    //@Value 获取配置文件里面的属性值，并且赋值给下面的属性

    @Value("${fileUpLoadPath}")
    String filePath;



    /**
     * @RequestMapping 表示同时支持接收post和get请求。
     * 如果加上method=RequestMethod.POST， 表示只接收post请求
     * 其实有一个注解可以直接指定，只接收post请求---@PostMapping
     * @param req
     * @param multiReq
     * @throws IOException
     */
    @PostMapping("/testUpload3")
    // @RequestMapping(value="/testUpload3",method=RequestMethod.POST)
    public void testUploadFile(HttpServletRequest req,MultipartHttpServletRequest multiReq) throws IOException{
        MultipartFile multipartFile= multiReq.getFile("file");
        String filename=multipartFile.getOriginalFilename();
        System.out.println("文件名字："+multipartFile.getOriginalFilename());
        File file=new File(filePath+filename);
        multipartFile.transferTo(file);
        /*String username=req.getParameter("username");
        System.out.println("用户名字："+username);*/

    }

}

